package org.enwey.icarenew.Fragment.Topic;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import org.enwey.icarenew.API.APIManager;
import org.enwey.icarenew.API.RequestListResult;
import org.enwey.icarenew.APPUtils.ToastUtil;
import org.enwey.icarenew.Activity.MainActivity;
import org.enwey.icarenew.Activity.News.NewDetailActivity;
import org.enwey.icarenew.Activity.News.TopicDetailActivity;
import org.enwey.icarenew.Adapter.TopicAdapter;
import org.enwey.icarenew.Fragment.BaseFragment.BaseFragment;
import org.enwey.icarenew.Model.Topic;
import org.enwey.icarenew.R;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

/**
 * Created by enwey on 2014/11/20.
 */
public class TopicFragment extends BaseFragment {
    private final String TAG = "TopicFragment";
    private TopicAdapter mAdapter;
    private List<Topic> mTopicList;
    private PullToRefreshListView mListView;
    private int currentPage = 0;
    private MainActivity mActivity;
    private boolean isFirst;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.fragment_topic, container, false);
        setUp();
        findView(view);
        registerListener();
        CallAPIGetTopicList();
        return view;
    }

    private void findView(View view) {
        mListView = (PullToRefreshListView) view.findViewById(R.id.fragment_topic_list);
        mListView.setAdapter(mAdapter);
    }

    @Override
    public void setContentView(LayoutInflater inflater, ViewGroup mLayerContextView) {
        inflater.inflate(R.layout.fragment_topic, mLayerContextView);
    }

    private void setUp() {
        mActivity = (MainActivity) getActivity();
        isFirst = true;
        mAdapter = new TopicAdapter(getActivity());
        mTopicList = new ArrayList<Topic>();
        mAdapter.setTopicList(mTopicList);
    }

    public void registerListener() {
        AdapterView.OnItemClickListener register = new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), TopicDetailActivity.class);
                intent.putExtra("topic_id", mTopicList.get(position - 1).id + "");
                startActivity(intent);
            }
        };
        mListView.setOnItemClickListener(register);

        mListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                currentPage = 0;
                CallAPIGetTopicList();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                if (mTopicList != null) {
                    currentPage++;
                    CallAPIGetTopicList();
                }
            }
        });

    }

    private void CallAPIGetTopicList() {
        if (isFirst) {
            mActivity.showLoading();
        }
        Hashtable<String, String> parameters = new Hashtable<>();
        parameters.put("page", currentPage + "");

        APIManager.getInstance(getActivity()).getTopicList(parameters, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                if (isFirst) {
                    mActivity.hideLoading();
                }
                mListView.onRefreshComplete();
                ToastUtil.show(getActivity(), "连接失败");
            }
        }, new Response.Listener<RequestListResult<Topic>>() {
            @Override
            public void onResponse(RequestListResult<Topic> response) {
                if (isFirst) {
                    isFirst = false;
                    mActivity.hideLoading();
                }
                if (response.data != null && response.data.size() > 0) {
                    if (currentPage == 0) {
                        mTopicList = response.data;
                        mAdapter.setTopicList(mTopicList);
                    } else if (currentPage > 0 && mTopicList != null) {
                        mTopicList.addAll(response.data);
                    }
                    mAdapter.notifyDataSetChanged();
                    mListView.onRefreshComplete();
                }

                if (mAdapter.getCount() == 0) {
                    mActivity.showNoData();
                } else if (response.code == 201 || response.data.size() < 10) {
                    mListView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
                    ToastUtil.show(getActivity(), "最后一页");
                } else {
                    mListView.setMode(PullToRefreshBase.Mode.BOTH);
                }
                mListView.onRefreshComplete();
            }
        });

    }

    @Override
    public void showFragment() {
        super.showFragment();
        CallAPIGetTopicList();
    }
}
